﻿using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using EPiServer.PlugIn;
using OpenWaves.EPiServer.CustomProperties;

namespace OpenWaves.EPiServer.GeoProperties
{
    [PropertyDefinitionTypePlugIn(DisplayName = "GeoRectangle Picker")]  
    public class PropertyGeoRectangle: StringProperty<GeoRectangle>
    {
        public const string UiHint = "ow.georectangle";
        public const string UniqueName = "propertyGeoRectangle";

        public PropertyGeoRectangle()
        {            
        }

        public PropertyGeoRectangle(GeoRectangle geoRectangle)
        {
            this.Value = geoRectangle;
        }

        protected override StringProperty<GeoRectangle> CreateInstance(GeoRectangle value)
        {
            return new PropertyGeoRectangle(value);
        }

        protected override string SerializeValue(GeoRectangle value)
        {
            return string.Format(CultureInfo.InvariantCulture, "{0};{1};{2}", value.NorthEast.ToSerializedString(),
                                 value.SouthWest.ToSerializedString(), value.MapZoom);
        }

        protected override GeoRectangle DeserializeValue(string serializedValue)
        {
            return GeoRectangle.Parse(serializedValue);
        }

        protected override GeoRectangle DefaultValue
        {
            get { return null; }
        }
    }

    [Serializable]
    public class GeoRectangle
    {
        public GeoPoint NorthEast { get; set; }

        public GeoPoint SouthWest { get; set; }

        public int MapZoom { get; set; }

        public GeoRectangle()
        {
            
        }

        public GeoRectangle(GeoPoint northEast, GeoPoint southWest, int mapZoom)
        {
            this.NorthEast = northEast;
            this.SouthWest = southWest;
            this.MapZoom = mapZoom;
        }

        public static GeoRectangle Parse(string value)
        {
            Contract.Requires(value != null, "Precondition failed");

            var stringArray = value.Split(';');            

            return new GeoRectangle(GeoPoint.Parse(stringArray[0]), GeoPoint.Parse(stringArray[1]), int.Parse(stringArray[2], CultureInfo.InvariantCulture));
        }
    }

    public static class GeoRectangleExtensions
    {
        public static string GetImageUrl(this GeoRectangle rectangle, int width, int height, string apiKey = null)
        {
            return String.Format(
                    "//maps.googleapis.com/maps/api/staticmap?&size={0}x{1}&visible={2},{3}|{4},{5}&sensor=false&key={6}",
                    width, height, rectangle.SouthWest.Latitude.ToString(CultureInfo.InvariantCulture), rectangle.SouthWest.Longitude.ToString(CultureInfo.InvariantCulture), rectangle.NorthEast.Latitude.ToString(CultureInfo.InvariantCulture), rectangle.NorthEast.Longitude.ToString(CultureInfo.InvariantCulture),
                    apiKey ?? ServiceLocator.Resolve<IGeoPropertyConfiguration>().GoogleMapsApiKey);
        }
    }
}